package com.atguigu.lease.web.admin.tasks;

import com.atguigu.lease.model.entity.LeaseAgreement;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.admin.service.LeaseAgreementService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class LeaseAgreementStatusTask {
    @Autowired
    private LeaseAgreementService leaseAgreementService;

    /**
     * @author gengjian
     * @date 2024/12/13 11:00:56
     * 定时任务注解简化配置 指定时间规则 每天检查
     * 秒 分 时 天 月 星期 年
     */
    @Scheduled(cron = "0 * * * * * ")
    private void updateLeaseAgreementStatus(){
        //创建实体类
        LeaseAgreement leaseAgreement = new LeaseAgreement();
        leaseAgreement.setStatus(LeaseStatus.EXPIRED);
        LambdaUpdateWrapper<LeaseAgreement> updateWrapper = new LambdaUpdateWrapper<>();
        //如果租期结束时间<当前时间，说明到期了 并且状态是在租状态才修改成已到期
        updateWrapper.lt(LeaseAgreement::getLeaseEndDate,new Date())
                .in(LeaseAgreement::getStatus,LeaseStatus.SIGNED,LeaseStatus.WITHDRAWING,LeaseStatus.RENEWING);
        //不装实体类不能自动填充修改时间
        leaseAgreementService.update(leaseAgreement,updateWrapper);
    }
}
